<?php

require_once 'classes/Pension.php';
require_once 'SCinterface.php';
require_once 'classes/Message.php';

/*
 * Classe de service CRUD pour manipuler les enregistrements de Pensions
 * @author Zikrija
 * @date 04.05.2012
 */
class SCpension {
    
    private $db;

    /**
     *Contructeur créant l'objet pour la connexion à la DB 
     */
    public function __construct() {
        $this->db = new PDO(ACA_DB_DSN, ACA_DB_USER, ACA_DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    /**
    * Ajoute une Pension
    * @param Pension Pension à ajouter
    */
    public function ajouter($unePensionAAjouter) {
        try{

            $query =  $this->db->prepare("INSERT INTO pension " .
                "(id, dateArrivee, dateDepart, montantParJour, fraisVeto, contrat_id) VALUES(".
                ":id, :dateArrivee, :dateDepart, :montantParJour, :fraisVeto, :contrat_id)");

            $query->execute(array(
                'id'              => $unePensionAAjouter->getId() ,
                'dateArrivee'     => $unePensionAAjouter->getDateArrivee(),
                'dateDepart'      => $unePensionAAjouter->getDateDepart() ,
                'montantParJour'  => $unePensionAAjouter->getMontantParJour() ,
                'fraisVeto'       => $unePensionAAjouter->getFraisVeto(),   
                'contrat_id'      => $unePensionAAjouter->getContrat_id()    
                ));
            $unePensionAAjouter->setId($this->db->lastInsertId());
            $query->closeCursor();
        }
        catch(PDOException $e){
            return new Message("Pension non ajoutée: ".$e->getMessage());
        }
        return $unePensionAAjouter;
   }
    /**
     * Methode permettant de modifier une Pension
     * @param Pension Pension à modifier
     * @return boolean 
     */
    public function modifier($unePensionAModifier){
        try{

            $query =  $this->db->prepare("UPDATE pension SET ".
                "dateArrivee = :dateArrivee, dateDepart = :dateDepart , montantParJour = :montantParJour, ".
                "fraisVeto = :fraisVeto, contrat_id = :contrat_id ".
                "WHERE id = :id");

            $query->execute(array(
                'id'              => $this->db->quote($unePensionAModifier->getId()) ,
                'dateArrivee'     => $this->db->quote($unePensionAModifier->getDateArrivee()),
                'dateDepart'      => $this->db->quote($unePensionAModifier->getDateDepart()) ,
                'montantParJour'  => $this->db->quote($unePensionAModifier->getMontantParJour()),
                'fraisVeto'       => $this->db->quote($unePensionAModifier->getFraisVeto()),   
                'contrat_id'      => $this->db->quote($unePensionAModifier->getContrat_id())
            ));

            $query->closeCursor();



        }
        catch(PDOException $e){
            return new Message("Pension non modifiée: ".$e->getMessage());
        }
        return $unePensionAModifier;
    }
    /**
     * Methode permettant de supprimer une Pension
     * @param int id de la Pension à supprimer
     * @return boolean 
     */
    public function supprimer($idPensionASupprimer){
        try{
            $query = $this->db->prepare("DELETE FROM pension WHERE id = :id");
            $query->execute(array(
                'id'           => $this->db->quote($idPensionASupprimer)
            ));            
            $query->closeCursor();
            
            $message = new Message("Pension supprimée"); 
        }
        catch(PDOException $e){
            $message = new Message("Pension non supprimée: ". $e->getMessage());
        }
        return $message;
    }
   
    /**
     * 
     * @param Pension de recherche sous forme d'objet Pension
     * @return un tableau d'objet Pension
     */
    public function lister($PensionALister){
        try{
            $sql="SELECT * FROM pension ";
            $sql_Where="WHERE";
            if($PensionALister!=NULL)
            {
                if($PensionALister->getId()!=NULL)
                    $sql_Where.=" id = '".$this->db->quote($PensionALister->getId())."' AND ";
                if($PensionALister->getDateArrivee()!=NULL)
                    $sql_Where.=" dateArrivee = '".$this->db->quote($PensionALister->getDateArrivee())."' AND ";
                if($PensionALister->getDateDepart()!=NULL)
                    $sql_Where.=" dateDepart = '".$this->db->quote($PensionALister->getDateDepart())."' AND ";
                if($PensionALister->getMontantParJour()!=NULL)
                    $sql_Where.=" montantParJour = '".$this->db->quote($PensionALister->getMontantParJour())."' AND ";
                if($PensionALister->getFraisVeto()!=NULL)
                    $sql_Where.=" fraisVeto = '".$this->db->quote($PensionALister->getFraisVeto())."' AND ";
                if($PensionALister->getContrat_id()!=NULL)
                    $sql_Where.=" contrat_id = '".$this->db->quote($PensionALister->getContrat_id())."' AND ";
            }         

            $sql_Where = substr($sql_Where,0,-5);
            //$sql.=$sql_Where." ORDER BY id";
            $sql.=$sql_Where;
            $lesPensionsTrouvees=NULL;
            $unePensionLue=NULL;
            
            //$sql.='INNER JOIN contrat ON contrat_id = '.
            $sql.=" ORDER BY id";
            $result=$this->db->query($sql);
            while($row = $result->fetch())
            {   
                $unePensionLue = new Pension($row['id'], $row['dateArrivee'], $row['dateDepart'], $row['montantParJour'], $row['fraisVeto'], $row['contrat_id']);

                $lesPensionsTrouvees[]=$unePensionLue;
            }                
        }
        catch(PDOException $e)
        {
            print($e->getMessage());
            $lesPensionsTrouvees=NULL;
        }
        return $lesPensionsTrouvees;
    } 
}

?>
